package logic

import (
	"context"
	"strings"

	"mall/common/globalkey"
	"mall/common/modelx"
	"mall/common/utils/pointy"
	"mall/service/attachment/model"
	"mall/service/attachment/rpc/internal/logic/utils/errorhandler"
	"mall/service/attachment/rpc/internal/svc"
	"mall/service/attachment/rpc/types/attachment"

	"github.com/zeromicro/go-zero/core/logx"
)

type BackendGetAttachmentLogListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBackendGetAttachmentLogListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendGetAttachmentLogListLogic {
	return &BackendGetAttachmentLogListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// ##25.05.13#
func (l *BackendGetAttachmentLogListLogic) BackendGetAttachmentLogList(in *attachment.AttachmentLogListReq) (*attachment.AttachmentLogListResp, error) {
	var orderBys []string
	var relOrderBys []string
	if len(in.OrderBy) > 0 {
		for _, orderBy := range in.OrderBy {
			order := " ASC"
			if orderBy.Order == "1" {
				order = " DESC"
			}
			if orderBy.Field == "id" {
				orderBys = append(orderBys, model.AttachmentLogColumns.LogID+order)
			}
			//if orderBy.Field == "sort" {
			//	orderBys = append(orderBys, model.AttachmentLogColumns.MenuOrder+order)
			//}
			if orderBy.Field == "createdAt" {
				orderBys = append(orderBys, model.AttachmentLogColumns.CreatedAt+order)
			}
		}
	}

	if len(orderBys) == 0 {
		orderBys = append(orderBys, model.AttachmentColumns.ID+" DESC")
	}

	var page uint64
	var pageSize uint64
	var restricted bool
	resp := &attachment.AttachmentLogListResp{}
	filterMap := make(map[string]map[string][]any)
	if in.Ids != nil {
		if vals := modelx.UniqueInterfaces(strings.TrimSpace(*in.Ids), 64); len(vals) > 0 {
			filterMap[model.AttachmentLogColumns.ObjectID] = map[string][]any{
				"in": vals,
			}
			restricted = true
		}
	}

	if in.RMQStep != nil && *in.RMQStep > 0 {
		filterMap[model.AttachmentLogColumns.RMQStep] = map[string][]any{
			"equalTo": {globalkey.RmqFinalStep},
		}
	}

	if !restricted {
		page = in.Page
		pageSize = in.PageSize
	}

	list, _, err := l.svcCtx.FileLog.GetList(l.ctx, model.M{
		"page":     page,
		"pageSize": pageSize,
		"orderBy":  strings.Join(relOrderBys, ","),
		//"select":   ",",
	}, 1, filterMap)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}
	resp.Total = uint64(len(list))
	if len(list) > 0 {
		for _, v := range list {
			resp.List = append(resp.List, &attachment.AttachmentLogInfo{
				Id:           pointy.GetPointer(v.LogID),
				AttachmentId: pointy.GetPointer(v.AttachmentID),
				ParentId:     pointy.GetPointer(v.ParentID),
				UserId:       pointy.GetPointer(v.UserID),
				Type:         pointy.GetPointer(uint32(v.Type)),
				Status:       pointy.GetPointer(uint32(v.Status)),
				RMQStep:      pointy.GetPointer(uint64(v.RMQStep)),
				Path:         pointy.GetPointer(v.Path),
				KeyValue:     pointy.GetPointer(v.KeyValue),
				TermId:       pointy.GetPointer(v.TermID),
				ObjectId:     pointy.GetPointer(v.ObjectID),
				ObjectType:   pointy.GetPointer(uint32(v.ObjectType)),
				Count:        pointy.GetPointer(v.Count),
				MetaValue:    pointy.GetPointer(v.MetaValue),
				CreateAt:     pointy.GetPointer(modelx.FormatTime(v.CreatedAt, "")),
			})
		}
	}
	return resp, nil
}
